package com.jz.manage.impl;

import java.nio.charset.Charset;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.transaction.annotation.Transactional;

import com.jz.dao.ModelDAO;
import com.jz.dao.TestDAO;
import com.jz.manage.ModelManage;
import com.jz.mm.ModelMm;
import com.jz.model.Model;
import com.jz.model.Test;

import flex.messaging.io.amf.ASObject;

public class ModelManageImpl implements ModelManage {
	
	private Log logger = LogFactory.getLog(ModelManageImpl.class);
	
	private ModelDAO modelDAO;
	private TestDAO testDao;

	private ModelMm modelMm;
	
	public void setModelMm(ModelMm modelMm) {
		this.modelMm = modelMm;
	}

	public void setModelDAO(ModelDAO modelDAO) {
		this.modelDAO = modelDAO;
	}

	public void deleteModel(Model model) {
		modelDAO.deleteModel(model);
	}

	public List<Model> getModels() {
		return modelDAO.getModels();
	}

//	public void insertModel(Model model) {
//		modelDAO.insertModel(model);
//	}

	public void updateModel(Model model) {
		modelDAO.updateModel(model);
	}

	@Transactional 
	public String putModel(ASObject as){
		Integer number = (Integer) as.get("number");
		Integer kind = (Integer) as.get("kind");
		String label = (String) as.get("label");
		String description = (String) as.get("description");
		
		Model model = (Model) modelMm.get(number);
		if (model != null) {
			System.out.println(Charset.defaultCharset());
			System.out.println("所得税的");
			
			logger.error("错误的写入 日志抓取" + number);
			return number + "已经存在！ ";
		}
		model = new Model();
		model.setNumber(number);
		model.setKind(kind);
		model.setLabel(label);
		model.setDescription(description);
		modelDAO.insertModel(model);
		modelMm.insert(model);
		
		
		
		return number +"成功存储";
		
	}
	
	
	@Transactional 
	public void insertModel(Model model){
		
		Integer number = model.getNumber();
		Model modelFind = modelDAO.getModelById(number);
		if (modelFind != null) {
//			System.out.println(Charset.defaultCharset());
//			System.out.println("所得税的");
			
			logger.error("错误的写入 日志抓取" + number);
			return;
		}
		
		modelDAO.insertModel(model);

		if(model.getTestList() != null){
			for(Test test : model.getTestList()){
				testDao.insertTest(test);
			}
		}
	}
	
	public String updateFlexModel(ASObject as){
		Integer number = (Integer) as.get("number");
		Integer kind = (Integer) as.get("kind");
		String label = (String) as.get("label");
		String description = (String) as.get("description");
		
		Model model = new Model();
		model.setNumber(number);
		model.setKind(kind);
		model.setLabel(label);
		model.setDescription(description);
		
		updateModel(model);
		return number +"成功存储";
		
	}
	
	
	public static void main(String[] args) {
		System.out.println("成功村上");
		System.out.println(Charset.defaultCharset());
	}

	public void setTestDao(TestDAO testDao) {
		this.testDao = testDao;
	}

	

}
